//
//  ReaderViewController.m
//  QRReadDemo
//
//  Created by Hailong.wang on 15/11/5.
//  Copyright © 2015年 Hailong.wang. All rights reserved.
//

#import "ReaderViewController.h"
#import <AVFoundation/AVFoundation.h>

//iOS7.0之后支持扫描二维码
@interface ReaderViewController () <AVCaptureMetadataOutputObjectsDelegate> {
    UILabel *_label;
}

//代表一种输入设备，麦克风，前后摄像头
//PropertyStrong(AVCaptureDevice, devices);
@property(nonatomic,strong) AVCaptureDevice *devices;
//输入设置

@property(nonatomic,strong) AVCaptureDeviceInput *input;
//代表会话，将输入输出结合起来

@property(nonatomic,strong) AVCaptureSession *session;
//代表某种特定数据类型的输出，譬如二维码，如果视频中出现二维码就立刻捕捉到

@property(nonatomic,strong) AVCaptureMetadataOutput *output;
//时时展示捕捉的视频流
@property(nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;

@end

@implementation ReaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self createView];
}

- (void)initData {
    //指定多媒体类型和默认设备defaultDevice指后置摄像头
    self.devices = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //输入设置进行输入设备的指定
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.devices error:nil];
    //暂时不指定数据输出的类型，在后面再指定
    self.output = [[AVCaptureMetadataOutput alloc] init];
    //设置输出的代理和执行的线程
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //初始化会话
    self.session = [[AVCaptureSession alloc] init];
    //为会话添加输入
    if ([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
    }
    //为会话添加输出
    if ([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];
    }
    //指定查找到二维码时输出
    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //设置添加预览层
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    //指定预览的区域
    self.previewLayer.frame = CGRectMake(10, 100, ScreenWidth - 2 * 10, ScreenWidth - 2 * 10);
    //指定区域视频填充模式
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:self.previewLayer];
    //奔跑吧兄弟
    [self.session startRunning];
}

- (void)createView {
    _label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.previewLayer.frame.origin.y+self.previewLayer.frame.size.height+40, ScreenWidth-20, ScreenHeight-self.previewLayer.frame.origin.y-self.previewLayer.frame.size.height-40)];
    _label.textColor = [UIColor blackColor];
    [self.view addSubview:_label];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    NSLog(@"%@", [[metadataObjects lastObject] class]);
    AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
    _label.text = object.stringValue;
    ILog(@"%@",object.stringValue);
}

@end
